<?php

$InstructionTitle = "Non-Prescriptions";
$InstructionDetails ="Are any of your non-prescription medications missing from this list? <UL> <LI> supplements (calcium, etc.) <LI> over-the-counter medications (aspirin, etc.) <LI> herbals (ginko, etc.)</UL>";

$BreadCrumb = TRUE;
$welcome = "BC-next";
$prescriptions = "BC-next";
$nonprescriptions = "BC-current";

// have to modify these more
$ButtonText = "Submit  &#160;&#160;  |||";
// this brings us to the wizard
$NextPage = "nonprescriptions.php";
$ContinuePage = "add-medication.php?NextPage=nonprescriptions.php&Wizard=1&NonPrescription=1";
$NextPage = "nonprescriptions.php";
$LargeButton1Text = "Yes &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;";
$LargeButton2Text = "Continue...&#160;&#160;";
?>


<?php
include 'includes/wizard-simple-header.php';
  include 'includes/db-login.php';

if (isset($_GET['AddMed'])){
  $AddMed = $_GET['AddMed']; 
  //  echo $AddMed;
  
  // add the medication sent to us
  list($MedicationIdentity, $GenericIngredients, $UnitStrength) = explode("~", $AddMed);

  $QueryPieces = explode("~", $AddMed);
  $InsertQueryPieces = explode("~", $AddMed);
  $QueryPiecesTotal = count($QueryPieces);
  $QueryPiecesCount = 0;
  
  // check for empty fields and replace them with NULL for database entry
  while ($QueryPiecesCount < $QueryPiecesTotal) {
    if (strlen($QueryPieces[$QueryPiecesCount]) == 0){
      $QueryPieces[$QueryPiecesCount] = " is NULL";
      $InsertQueryPieces[$QueryPiecesCount] = "NULL";
    } else {
      $QueryPieces[$QueryPiecesCount] = " = '".$QueryPieces[$QueryPiecesCount]."'";
      $InsertQueryPieces[$QueryPiecesCount] = "'".$InsertQueryPieces[$QueryPiecesCount]."'";
    }
    $QueryPiecesCount++;
  } // while $QueryPiecesCount

      
  //Put it in the medication list
  // QUESTION: not sure if AdHoc is correct b/c it's nonprescription
  $query = "SELECT * FROM medications WHERE MedicationType = 'AdHoc' AND MedicationIdentity ".$QueryPieces[0]." AND GenericIngredients ".$QueryPieces[1]." AND UnitStrength ".$QueryPieces[2];
  //  echo $query;


  $result = mysql_query($query) or die('Query failed: ' . mysql_error());

  $numresults = mysql_num_rows($result);

  // If medication not in list (no med coming up with that name), put it in the list
  // If the medication is in the database, change inList to TRUE once
  if ($numresults == 0){
    //inserting into DB
    $inList = "TRUE";
    //INSERT INTO medications (MedicationType, MedicationIdentity,
    //GenericIngredients, UnitStrength, inList) VALUES
    //('prescription', 'Premarin', NULL, NULL, TRUE);
    // May have to add in the field where this is data is from the patient
    $query = "INSERT INTO medications (MedicationType, MedicationIdentity,
GenericIngredients, UnitStrength, inList, Provenance) VALUES ('AdHoc',".$InsertQueryPieces[0].", ".$InsertQueryPieces[1].", ".$InsertQueryPieces[2].", ".$inList.", 'PersonalSource')";	
    
    //    echo "<BR>numResults=0: ".$query."<BR>";

    $InsertResult = mysql_query($query) or die('Query failed: ' . mysql_error());
  } else {
    $row = mysql_fetch_array($result);
    //Should only be one in the database, but there may be more
    // We're only going to set one InList to true so there aren't multiple meds on the list
    $query = "UPDATE medications SET inList = TRUE WHERE ID = ".$row['ID'];
    $UpdateResult = mysql_query($query) or die('Query failed: ' . mysql_error());
  }

 } // end if isset AddMed

  // have to create a med list from what is already there...
include 'includes/create-med-list.php';

include 'includes/db-close.php';
include 'includes/wizard-large-button.php';
include 'includes/wizard-simple-footer.php';
?>
